#include "Map.hpp"
#include "Game.hpp"
#include <numeric>
#include <iostream>
#include <random>
#include <algorithm>

sf::Texture Map::wallTexture;
sf::Texture Map::wallDestructibleTexture;
sf::Texture Map::grassTexture;
sf::Texture Map::grassFlowerTexture;
sf::Texture Map::iceTexture;
sf::Texture Map::strongWallTexture;
sf::Texture Map::damagedStrongWallTexture;
bool Map::texturesLoaded = false;

void Map::loadTextures() {
    if (!texturesLoaded) {
        if (!wallTexture.loadFromFile("resources/textures/wall.png")) {
            throw std::runtime_error("Failed to load wall texture");
        }
        if (!wallDestructibleTexture.loadFromFile("resources/textures/wall_destructible.png")) {
            throw std::runtime_error("Failed to load destructible wall texture");
        }
        if (!grassTexture.loadFromFile("resources/textures/grass.png")) {
            throw std::runtime_error("Failed to load grass texture");
        }
        if (!grassFlowerTexture.loadFromFile("resources/textures/grass_flower.png")) {
            throw std::runtime_error("Failed to load grass flower texture");
        }
        if (!iceTexture.loadFromFile("resources/textures/ice.png")) {
            throw std::runtime_error("Failed to load ice texture");
        }
        if (!strongWallTexture.loadFromFile("resources/textures/strong_wall.png")) {
            throw std::runtime_error("Failed to load strong wall texture");
        }
        if (!damagedStrongWallTexture.loadFromFile("resources/textures/strong_wall_damaged.png")) {
            throw std::runtime_error("Failed to load damaged strong wall texture");
        }
        texturesLoaded = true;
    }
}

Map::Map(const sf::Vector2u& screenSize) {
    // 加载音效
    if (!mHitSoundBuffer.loadFromFile("resources/audio/hit.wav")) {
        throw std::runtime_error("Failed to load hit sound");
    }
    if (!mExplosionSoundBuffer.loadFromFile("resources/audio/explosion.wav")) {
        throw std::runtime_error("Failed to load explosion sound");
    }
    mHitSound.setBuffer(mHitSoundBuffer);
    mExplosionSound.setBuffer(mExplosionSoundBuffer);
    loadTextures();
    
    // 计算地图大小为屏幕面积的1/4
    float mapArea = screenSize.x * screenSize.y * 0.25f;
    float aspectRatio = static_cast<float>(screenSize.x) / screenSize.y;
    
    // 保持地图的宽高比与屏幕相同
    mMapSize.y = std::sqrt(mapArea / aspectRatio);
    mMapSize.x = mMapSize.y * aspectRatio;
    
    // 计算地图中心点（屏幕中心）
    mMapCenter = sf::Vector2f(screenSize.x / 2.f, screenSize.y / 2.f);
    
    // 计算地图左上角位置（使地图居中）
    float mapLeft = mMapCenter.x - mMapSize.x / 2.f;
    float mapTop = mMapCenter.y - mMapSize.y / 2.f;
    
    createBorder();
}

void Map::createBorder() {
    // 计算整个地图的边界位置（不留边距）
    float mapLeft = 0;
    float mapRight = mMapCenter.x * 2;  // 整个屏幕宽度
    float mapTop = 0;
    float mapBottom = mMapCenter.y * 2;  // 整个屏幕高度
    
    // 计算需要的墙体数量
    int horizontalWalls = static_cast<int>(std::ceil(mapRight / WALL_WIDTH));
    int verticalWalls = static_cast<int>(std::ceil(mapBottom / WALL_HEIGHT));
    
    // 创建边界墙（使用StrongWall类型）
    // 上边界
    for (int i = 0; i < horizontalWalls; ++i) {
        float x = mapLeft + i * WALL_WIDTH;
        createWallGroup(sf::Vector2f(x, mapTop), 1, 1, TileType::StrongWall);
    }
    
    // 下边界
    for (int i = 0; i < horizontalWalls; ++i) {
        float x = mapLeft + i * WALL_WIDTH;
        createWallGroup(sf::Vector2f(x, mapBottom - WALL_HEIGHT), 1, 1, TileType::StrongWall);
    }
    
    // 左边界（不包括已经在上下边界的墙）
    for (int i = 1; i < verticalWalls - 1; ++i) {
        float y = mapTop + i * WALL_HEIGHT;
        createWallGroup(sf::Vector2f(mapLeft, y), 1, 1, TileType::StrongWall);
    }
    
    // 右边界（不包括已经在上下边界的墙）
    for (int i = 1; i < verticalWalls - 1; ++i) {
        float y = mapTop + i * WALL_HEIGHT;
        createWallGroup(sf::Vector2f(mapRight - WALL_WIDTH, y), 1, 1, TileType::StrongWall);
    }
}

void Map::createWallGroup(const sf::Vector2f& position, int width, int height, TileType type) {
    for (int y = 0; y < height; ++y) {
        for (int x = 0; x < width; ++x) {
            sf::Vector2f pos(
                position.x + x * WALL_WIDTH,
                position.y + y * WALL_HEIGHT
            );
            
            // 确保在地图范围内
            float mapLeft = mMapCenter.x - mMapSize.x / 2.f;
            float mapRight = mMapCenter.x + mMapSize.x / 2.f;
            float mapTop = mMapCenter.y - mMapSize.y / 2.f;
            float mapBottom = mMapCenter.y + mMapSize.y / 2.f;
            
            if (pos.x < mapLeft || pos.x > mapRight - WALL_WIDTH ||
                pos.y < mapTop || pos.y > mapBottom - WALL_HEIGHT) {
                continue;
            }
            
            createWall(pos, type);
        }
    }
}

void Map::loadDefaultMap() {
    // 计算相对位置
    float margin = 40.f;  // 保持40像素的边距
    float mapLeft = mMapCenter.x - mMapSize.x / 2.f + margin;
    float mapRight = mMapCenter.x + mMapSize.x / 2.f - margin;
    float mapTop = mMapCenter.y - mMapSize.y / 2.f + margin;
    float mapBottom = mMapCenter.y + mMapSize.y / 2.f - margin;
    
    // 中央十字形障碍
    float centerX = mMapCenter.x;
    float centerY = mMapCenter.y;
    createWallGroup(sf::Vector2f(centerX - WALL_WIDTH * 1.5f, centerY), 3, 2, TileType::DestructibleWall);
    createWallGroup(sf::Vector2f(centerX - WALL_WIDTH, centerY - WALL_HEIGHT), 2, 4, TileType::DestructibleWall);
    
    // 添加2-3个随机的强化砖块区域
    int numStrongWallAreas = 2 + (rand() % 2);  // 2-3个强化砖块区域
    for (int i = 0; i < numStrongWallAreas; ++i) {
        // 在地图范围内随机选择位置，确保对齐到网格
        float availableWidth = mapRight - mapLeft - WALL_WIDTH * 2;
        float availableHeight = mapBottom - mapTop - WALL_HEIGHT * 2;
        float x = mapLeft + ((rand() % static_cast<int>(availableWidth / WALL_WIDTH)) * WALL_WIDTH);
        float y = mapTop + ((rand() % static_cast<int>(availableHeight / WALL_HEIGHT)) * WALL_HEIGHT);
        int width = 2 + (rand() % 2);  // 2-3个单位宽
        int height = 2 + (rand() % 2); // 2-3个单位高
        createWallGroup(sf::Vector2f(x, y), width, height, TileType::StrongWall);
    }
    
    // 添加3-5个随机的大型墙块区域
    int numWallAreas = 3 + (rand() % 3);  // 3-5个墙块区域
    for (int i = 0; i < numWallAreas; ++i) {
        // 在地图范围内随机选择位置
        float availableWidth = mapRight - mapLeft - WALL_WIDTH * 4;
        float availableHeight = mapBottom - mapTop - WALL_HEIGHT * 4;
        float x = mapLeft + ((rand() % static_cast<int>(availableWidth / WALL_WIDTH)) * WALL_WIDTH);
        float y = mapTop + ((rand() % static_cast<int>(availableHeight / WALL_HEIGHT)) * WALL_HEIGHT);
        
        // 随机决定区域大小
        int width = 3 + (rand() % 3);   // 3-5个单位宽
        int height = 3 + (rand() % 3);  // 3-5个单位高
        
        createRandomWallArea(sf::Vector2f(x, y), width, height);
    }
    
    // 添加6-8个随机的草地区域
    int numGrassAreas = 6 + (rand() % 3);  // 6-8个草地区域
    for (int i = 0; i < numGrassAreas; ++i) {
        // 在地图范围内随机选择位置，确保对齐到网格
        float availableWidth = mapRight - mapLeft - WALL_WIDTH * 2;
        float availableHeight = mapBottom - mapTop - WALL_HEIGHT * 2;
        float x = mapLeft + ((rand() % static_cast<int>(availableWidth / WALL_WIDTH)) * WALL_WIDTH);
        float y = mapTop + ((rand() % static_cast<int>(availableHeight / WALL_HEIGHT)) * WALL_HEIGHT);
        createGrassArea(sf::Vector2f(x, y), 1);  // 每个区域1x1大小
    }
    
    // 定义边缘位置的生成点
    std::vector<sf::Vector2f> edgePoints = {
        sf::Vector2f(mapLeft + WALL_WIDTH * 2, mapTop + WALL_HEIGHT * 2),      // 左上
        sf::Vector2f(mapRight - WALL_WIDTH * 4, mapTop + WALL_HEIGHT * 2),     // 右上
        sf::Vector2f(mapLeft + WALL_WIDTH * 2, mapBottom - WALL_HEIGHT * 4),   // 左下
        sf::Vector2f(mapRight - WALL_WIDTH * 4, mapBottom - WALL_HEIGHT * 4)   // 右下
    };
    
    // 随机选择2个边缘位置放置冰块
    std::vector<int> availableEdges(edgePoints.size());
    std::iota(availableEdges.begin(), availableEdges.end(), 0);
    
    // 创建随机数生成器
    std::random_device rd;
    std::mt19937 gen(rd());
    std::shuffle(availableEdges.begin(), availableEdges.end(), gen);
    
    // 在选定的边缘位置创建冰块区域
    for (int i = 0; i < 2; ++i) {
        sf::Vector2f edgePos = edgePoints[availableEdges[i]];
        createIceArea(edgePos, 3);  // 在边缘创建较大的冰块区域
    }
    
    // 添加1-2个随机位置的冰块区域
    int numRandomGroups = 1 + (rand() % 2);
    for (int i = 0; i < numRandomGroups; ++i) {
        // 为随机组选择中心点
        float centerX = mapLeft + WALL_WIDTH * 4 +
            (rand() % static_cast<int>((mapRight - mapLeft - WALL_WIDTH * 8) / WALL_WIDTH)) * WALL_WIDTH;
        float centerY = mapTop + WALL_HEIGHT * 4 +
            (rand() % static_cast<int>((mapBottom - mapTop - WALL_HEIGHT * 8) / WALL_HEIGHT)) * WALL_HEIGHT;
        
        // 创建主要冰块区域
        createIceArea(sf::Vector2f(centerX, centerY), 3);
        
        // 添加2-3个卫星小冰块区域
        int numSatellites = 2 + (rand() % 2);
        for (int j = 0; j < numSatellites; ++j) {
            float offsetX = (rand() % 7 - 3) * WALL_WIDTH;
            float offsetY = (rand() % 7 - 3) * WALL_HEIGHT;
            sf::Vector2f satellitePos(centerX + offsetX, centerY + offsetY);
            createIceArea(satellitePos, 2);  // 创建较小的卫星区域
        }
    }
}

void Map::createWall(const sf::Vector2f& position, TileType type, bool useFlowerTexture) {
    Wall wall;
    switch (type) {
        case TileType::Wall:
            wall.sprite.setTexture(wallTexture);
            wall.health = 1;
            break;
        case TileType::DestructibleWall:
            wall.sprite.setTexture(wallDestructibleTexture);
            wall.health = 1;
            break;
        case TileType::Grass:
            wall.sprite.setTexture(useFlowerTexture ? grassFlowerTexture : grassTexture);
            wall.health = 1;
            break;
        case TileType::Ice:
            wall.sprite.setTexture(iceTexture);
            wall.health = 1;
            break;
        case TileType::StrongWall:
            wall.sprite.setTexture(strongWallTexture);
            wall.health = 2;  // 强化砖块有2点生命值
            break;
    }
    wall.sprite.setPosition(position);
    wall.type = type;
    wall.active = true;
    wall.damaged = false;
    mWalls.push_back(wall);
}

bool Map::isInGrass(const sf::FloatRect& bounds) const {
    for (const auto& wall : mWalls) {
        if (wall.active && wall.type == TileType::Grass &&
            wall.sprite.getGlobalBounds().intersects(bounds)) {
            return true;
        }
    }
    return false;
}

bool Map::isOnIce(const sf::FloatRect& bounds) const {
    for (const auto& wall : mWalls) {
        if (wall.active && wall.type == TileType::Ice &&
            wall.sprite.getGlobalBounds().intersects(bounds)) {
            return true;
        }
    }
    return false;
}

void Map::render(sf::RenderWindow& window) {
    for (const auto& wall : mWalls) {
        if (wall.active) {
            window.draw(wall.sprite);
        }
    }
}

bool Map::checkCollision(const sf::FloatRect& bounds) const {
    for (const auto& wall : mWalls) {
        if (wall.active &&
            wall.type != TileType::Grass &&
            wall.type != TileType::Ice &&
            wall.sprite.getGlobalBounds().intersects(bounds)) {
            return true;
        }
    }
    return false;
}

bool Map::handleBulletCollision(const sf::FloatRect& bulletBounds) {
    for (auto& wall : mWalls) {
        if (wall.active && wall.sprite.getGlobalBounds().intersects(bulletBounds)) {
            if (wall.type == TileType::DestructibleWall) {
                wall.active = false;  // 摧毁可破坏的墙
                playExplosionSound();  // 播放爆炸音效
            } else if (wall.type == TileType::Grass || wall.type == TileType::Ice) {
                return false;  // 子弹可以穿过草地和冰块
            } else if (wall.type == TileType::StrongWall) {
                wall.health--;  // 减少强化砖块的生命值
                if (wall.health == 1 && !wall.damaged) {
                    // 第一次击中，切换到受损贴图
                    wall.sprite.setTexture(damagedStrongWallTexture);
                    wall.damaged = true;
                    playHitSound();  // 播放击中音效
                } else if (wall.health <= 0) {
                    // 第二次击中，摧毁砖块
                    wall.active = false;
                    playExplosionSound();  // 播放爆炸音效
                }
            } else {
                // 普通墙（边界墙）
                playExplosionSound();  // 播放爆炸音效
            }
            return true;  // 发生碰撞
        }
    }
    return false;  // 没有碰撞
}

void Map::playHitSound() {
    mHitSound.play();
}

void Map::playExplosionSound() {
    mExplosionSound.play();
}

bool Map::canPlaceAt(const sf::Vector2f& position, TileType type) const {
    // 首先检查是否在地图边界内
    float margin = 40.f;  // 保持40像素的边距
    float mapLeft = mMapCenter.x - mMapSize.x / 2.f + margin;
    float mapRight = mMapCenter.x + mMapSize.x / 2.f - margin;
    float mapTop = mMapCenter.y - mMapSize.y / 2.f + margin;
    float mapBottom = mMapCenter.y + mMapSize.y / 2.f - margin;
    
    if (position.x < mapLeft || position.x > mapRight - WALL_WIDTH ||
        position.y < mapTop || position.y > mapBottom - WALL_HEIGHT) {
        return false;
    }
    
    sf::FloatRect newBounds(position.x, position.y, WALL_WIDTH, WALL_HEIGHT);
    
    // 扩展检查区域，防止区域之间太近
    sf::FloatRect extendedBounds = newBounds;
    extendedBounds.left -= WALL_WIDTH * 0.5f;
    extendedBounds.top -= WALL_HEIGHT * 0.5f;
    extendedBounds.width += WALL_WIDTH;
    extendedBounds.height += WALL_HEIGHT;
    
    for (const auto& wall : mWalls) {
        if (wall.active) {
            // 检查是否与任何现有元素重叠
            if (wall.sprite.getGlobalBounds().intersects(newBounds)) {
                return false;
            }
            
            // 如果是不同类型的元素，还要检查扩展区域
            if (wall.type != type &&
                wall.sprite.getGlobalBounds().intersects(extendedBounds)) {
                return false;
            }
        }
    }
    return true;
}

void Map::createGrassArea(const sf::Vector2f& center, int size) {
    // 创建一个size x size的草地区域
    for (int dy = -size/2; dy <= size/2; ++dy) {
        for (int dx = -size/2; dx <= size/2; ++dx) {
            sf::Vector2f pos(
                center.x + dx * WALL_WIDTH,
                center.y + dy * WALL_HEIGHT
            );
            
            // 确保在地图范围内，留出边距
            float margin = 40.f;  // 保持40像素的边距
            float mapLeft = mMapCenter.x - mMapSize.x / 2.f + margin;
            float mapRight = mMapCenter.x + mMapSize.x / 2.f - margin;
            float mapTop = mMapCenter.y - mMapSize.y / 2.f + margin;
            float mapBottom = mMapCenter.y + mMapSize.y / 2.f - margin;
            
            if (pos.x < mapLeft || pos.x > mapRight || pos.y < mapTop || pos.y > mapBottom) {
                continue;
            }
            
            // 检查是否可以放置
            if (canPlaceAt(pos, TileType::Grass)) {
                // 20%的概率使用带花的贴图
                bool useFlower = (rand() % 5) == 0;
                createWall(pos, TileType::Grass, useFlower);
            }
        }
    }
}

void Map::createIceArea(const sf::Vector2f& center, int size) {
    const int UNIT_SIZE = 2;  // 基本冰块单元大小为2x2
    const int NUM_UNITS = size;  // 要创建的冰块单元数量
    
    // 创建多个冰块单元
    for (int unit = 0; unit < NUM_UNITS; ++unit) {
        // 为每个单元计算一个相对于中心的偏移位置
        int offsetX = (rand() % 5 - 2) * UNIT_SIZE * WALL_WIDTH;  // -2到2个单元的偏移
        int offsetY = (rand() % 5 - 2) * UNIT_SIZE * WALL_HEIGHT;
        
        sf::Vector2f unitCenter(center.x + offsetX, center.y + offsetY);
        
        // 创建一个2x2的基本冰块单元
        for (int dy = -UNIT_SIZE/2; dy <= UNIT_SIZE/2; ++dy) {
            for (int dx = -UNIT_SIZE/2; dx <= UNIT_SIZE/2; ++dx) {
                // 添加一些随机性来创建不规则形状
                if (std::abs(dx) == UNIT_SIZE/2 && std::abs(dy) == UNIT_SIZE/2) {
                    // 角落位置有25%的概率不放置冰块
                    if (rand() % 4 == 0) continue;
                }
                
                sf::Vector2f pos(
                    unitCenter.x + dx * WALL_WIDTH,
                    unitCenter.y + dy * WALL_HEIGHT
                );
                
                // 确保在地图范围内
                float margin = 40.f;
                float mapLeft = mMapCenter.x - mMapSize.x / 2.f + margin;
                float mapRight = mMapCenter.x + mMapSize.x / 2.f - margin;
                float mapTop = mMapCenter.y - mMapSize.y / 2.f + margin;
                float mapBottom = mMapCenter.y + mMapSize.y / 2.f - margin;
                
                if (pos.x < mapLeft || pos.x > mapRight || pos.y < mapTop || pos.y > mapBottom) {
                    continue;
                }
                
                // 检查是否可以放置
                if (canPlaceAt(pos, TileType::Ice)) {
                    createWall(pos, TileType::Ice);
                }
            }
        }
    }
    
    // 在单元之间添加连接冰块
    int numConnectors = NUM_UNITS * 3;  // 每个单元平均3个连接块
    for (int i = 0; i < numConnectors; ++i) {
        // 在更大范围内随机选择位置
        int dx = (rand() % 7 - 3) * WALL_WIDTH;  // -3到3的范围
        int dy = (rand() % 7 - 3) * WALL_HEIGHT;
        
        sf::Vector2f pos(
            center.x + dx,
            center.y + dy
        );
        
        // 确保在地图范围内
        float margin = 40.f;
        float mapLeft = mMapCenter.x - mMapSize.x / 2.f + margin;
        float mapRight = mMapCenter.x + mMapSize.x / 2.f - margin;
        float mapTop = mMapCenter.y - mMapSize.y / 2.f + margin;
        float mapBottom = mMapCenter.y + mMapSize.y / 2.f - margin;
        
        if (pos.x < mapLeft || pos.x > mapRight || pos.y < mapTop || pos.y > mapBottom) {
            continue;
        }
        
        // 检查周围是否有其他冰块
        bool hasNearbyIce = false;
        for (const auto& wall : mWalls) {
            if (wall.active && wall.type == TileType::Ice) {
                sf::Vector2f diff = wall.sprite.getPosition() - pos;
                float distance = std::sqrt(diff.x * diff.x + diff.y * diff.y);
                if (distance <= WALL_WIDTH * 1.5f) {  // 如果附近有冰块
                    hasNearbyIce = true;
                    break;
                }
            }
        }
        
        // 只在靠近现有冰块的地方添加连接块
        if (hasNearbyIce && canPlaceAt(pos, TileType::Ice)) {
            createWall(pos, TileType::Ice);
        }
    }
}

void Map::createRandomWallArea(const sf::Vector2f& center, int width, int height) {
    // 创建一个不规则的墙块区域
    for (int dy = -height/2; dy <= height/2; ++dy) {
        for (int dx = -width/2; dx <= width/2; ++dx) {
            // 添加随机性来创建不规则形状
            
            // 角落位置有75%的概率不放置墙
            if ((std::abs(dx) == width/2 && std::abs(dy) == height/2) && (rand() % 4 < 3)) {
                continue;
            }
            
            // 边缘位置有50%的概率不放置墙
            if ((std::abs(dx) == width/2 || std::abs(dy) == height/2) && (rand() % 2 == 0)) {
                continue;
            }
            
            // 中心区域有25%的概率不放置墙，创造"孔洞"
            if (std::abs(dx) < width/2 && std::abs(dy) < height/2 && (rand() % 4 == 0)) {
                continue;
            }
            
            sf::Vector2f pos(
                center.x + dx * WALL_WIDTH,
                center.y + dy * WALL_HEIGHT
            );
            
            // 确保在地图范围内，留出边距
            float margin = 40.f;
            float mapLeft = mMapCenter.x - mMapSize.x / 2.f + margin;
            float mapRight = mMapCenter.x + mMapSize.x / 2.f - margin;
            float mapTop = mMapCenter.y - mMapSize.y / 2.f + margin;
            float mapBottom = mMapCenter.y + mMapSize.y / 2.f - margin;
            
            if (pos.x < mapLeft || pos.x > mapRight || pos.y < mapTop || pos.y > mapBottom) {
                continue;
            }
            
            // 检查是否可以放置
            if (canPlaceAt(pos, Map::TileType::Wall)) {
                // 随机选择墙的类型：70%普通墙，30%可破坏墙
                Map::TileType wallType = (rand() % 10 < 7) ? Map::TileType::Wall : Map::TileType::DestructibleWall;
                createWall(pos, wallType);
            }
        }
    }
    
    // 在区域周围随机添加一些额外的墙，创造更自然的外观
    int extraWalls = rand() % ((width + height) / 2);  // 添加一些额外的墙
    for (int i = 0; i < extraWalls; ++i) {
        int dx = (rand() % (width + 2)) - (width + 1)/2;
        int dy = (rand() % (height + 2)) - (height + 1)/2;
        
        sf::Vector2f pos(
            center.x + dx * WALL_WIDTH,
            center.y + dy * WALL_HEIGHT
        );
        
        // 确保在地图范围内
        float margin = 40.f;
        float mapLeft = mMapCenter.x - mMapSize.x / 2.f + margin;
        float mapRight = mMapCenter.x + mMapSize.x / 2.f - margin;
        float mapTop = mMapCenter.y - mMapSize.y / 2.f + margin;
        float mapBottom = mMapCenter.y + mMapSize.y / 2.f - margin;
        
        if (pos.x < mapLeft || pos.x > mapRight || pos.y < mapTop || pos.y > mapBottom) {
            continue;
        }
        
        if (canPlaceAt(pos, Map::TileType::Wall)) {
            // 随机选择墙的类型：70%普通墙，30%可破坏墙
            Map::TileType wallType = (rand() % 10 < 7) ? Map::TileType::Wall : Map::TileType::DestructibleWall;
            createWall(pos, wallType);
        }
    }
}

void Map::createStrongWallArea(const sf::Vector2f& center, int size) {
    // 创建一个size x size的强化砖块区域
    for (int dy = -size/2; dy <= size/2; ++dy) {
        for (int dx = -size/2; dx <= size/2; ++dx) {
            sf::Vector2f pos(
                center.x + dx * WALL_WIDTH,
                center.y + dy * WALL_HEIGHT
            );
            
            // 确保在地图范围内，留出边距
            float margin = 40.f;  // 保持40像素的边距
            float mapLeft = mMapCenter.x - mMapSize.x / 2.f + margin;
            float mapRight = mMapCenter.x + mMapSize.x / 2.f - margin;
            float mapTop = mMapCenter.y - mMapSize.y / 2.f + margin;
            float mapBottom = mMapCenter.y + mMapSize.y / 2.f - margin;
            
            if (pos.x < mapLeft || pos.x > mapRight || pos.y < mapTop || pos.y > mapBottom) {
                continue;
            }
            
            // 检查是否可以放置
            if (canPlaceAt(pos, TileType::StrongWall)) {
                createWall(pos, TileType::StrongWall);
            }
        }
    }
}
